package com.example.work4.view;

import android.content.Context;
import android.os.Looper;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.example.work4.R;
import com.example.work4.data.Record;
import com.example.work4.data.RecordDataBase;
import com.example.work4.po.Moments;
import com.xuexiang.xui.widget.dialog.materialdialog.DialogAction;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;
import com.xuexiang.xui.widget.dialog.materialdialog.simplelist.MaterialSimpleListAdapter;
import com.xuexiang.xui.widget.dialog.materialdialog.simplelist.MaterialSimpleListItem;

import java.util.ArrayList;
import java.util.List;

public class MyDialog {

    private  Context context;

    private  MaterialDialog materialDialog;


    public MyDialog(Context context)
    {
        this.context=context;
    }

    public void ShowDialog2(Moments moments) {
        materialDialog=new MaterialDialog.Builder(context)
                .customView(R.layout.dialog, true)
                .iconRes(R.mipmap.ic_launcher)
                .title("查看")

                .positiveText("OK")
                .negativeText("取消")
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                EditText title=materialDialog.getView().findViewById(R.id.title);
                                EditText content=materialDialog.getView().findViewById(R.id.content);
                                Record record=new Record(null,title.getText().toString(),content.getText().toString());
                                RecordDataBase.Companion.getDatabase(context,"dlg").recordDao().insert(record);
                                Looper.prepare();
                                Toast.makeText(context,"添加完毕",Toast.LENGTH_SHORT).show();
                                Looper.loop();
                            }
                        }).start();
                    }
                })
                .show();
    }

    public void ShowDialog() {
        materialDialog=new MaterialDialog.Builder(context)
                .customView(R.layout.dialog, true)
                .iconRes(R.mipmap.ic_launcher)
                .title("新增")
                .positiveText("OK")
                .negativeText("取消")
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                EditText title=materialDialog.getView().findViewById(R.id.title);
                                EditText content=materialDialog.getView().findViewById(R.id.content);
                                Record record=new Record(null,title.getText().toString(),content.getText().toString());
                                RecordDataBase.Companion.getDatabase(context,"dlg").recordDao().insert(record);
                                Looper.prepare();
                                Toast.makeText(context,"添加完毕",Toast.LENGTH_SHORT).show();
                                Looper.loop();
                            }
                        }).start();
                    }
                })
                .show();
    }

    public void showRecordDialog(Record record)
    {
        List<MaterialSimpleListItem> list = new ArrayList<>();
        list.add(new MaterialSimpleListItem.Builder(context)
                .content("编号："+record.getId())
                .build());
        list.add(new MaterialSimpleListItem.Builder(context)
                .content("题目："+record.getTitle())
                .build());
        list.add(new MaterialSimpleListItem.Builder(context)
                .content("内容："+record.getContent())
                .build());
        final MaterialSimpleListAdapter adapter = new MaterialSimpleListAdapter(list)
                .setOnItemClickListener(new MaterialSimpleListAdapter.OnItemClickListener() {
                    @Override
                    public void onMaterialListItemSelected(MaterialDialog dialog, int index, MaterialSimpleListItem item) {

                    }
                });
        new MaterialDialog.Builder(context).title("详情：").iconRes(R.mipmap.ic_launcher).positiveText("OK").adapter(adapter, null).show();
    }
}
